<?php

require_once '../config.php';
require_once '../functions.php';

$size = 3;
$page = empty($_GET['page']) ? 1: (int)$_GET['page'];


if($page < 1 ){
    // 不可能有小于1
    header('Location: /admin/index.php?page=1');
    // header('Location: ?page=1');
}



$total_count = book_fetch_one('select 
count(1) as num
    from list
inner join options on options.option_isbn=list.list_isbn
;')['num'];
$total_pages = (int)ceil($total_count / $size);
// 计算越过多少条
$offset = ($page -1)* $size;


$current_user=books_get_current_user();

$list = book_fetch_all("select 
    list.id,
    list.title,
    list.picture,
    list.synopsis,
    list.number,
    list.created,
    options.discussnum
    from list
    inner join options on options.option_isbn=list.list_isbn
    order by list.id asc
    limit {$offset},{$size};");

// 最大页数 $total_pages = ceil($total_count/size)



$visiables = 5;

// 计算最大和最小展示的页码
$begin = $page - ($visiables - 1) / 2;
$end = $begin + $visiables - 1;

// 重点考虑合理性的问题
// begin > 0  end <= total_pages
$begin = $begin < 1 ? 1 : $begin; // 确保了 begin 不会小于 1
$end = $begin + $visiables - 1; // 因为 50 行可能导致 begin 变化，这里同步两者关系
$end = $end > $total_pages ? $total_pages : $end; // 确保了 end 不会大于 total_pages
$begin = $end - $visiables + 1; // 因为 52 可能改变了 end，也就有可能打破 begin 和 end 的关系
$begin = $begin < 1 ? 1 : $begin; // 确保不能小于 1



// // 计算页码
// $visiables =5;
// $region = ($visiables - 1)/2;  //左右区间
// $begin = $page - $region;   //开始页码
// $end = $page + $visiables ;  //结束页码 +  1

// // $benin 必须>0,确保最小为1

// if($begin < 1){
//     // $begin 修改 意味着必须要给 $end
//     $begin = 1;
//     $end = $begin +$visiables;
// };
// // $end 必须 <= 最大页数


// if($end > $total_pages + 1 ){
//     // end 超出范围
//     $end = $total_pages + 1;
//     $begin = $end-$visiables;
// };



$book_list = book_fetch_all('select * from bookitem;');

// 火热文章排序
$hot_list = book_fetch_all("select 
    list.id,title,number 
    from list 
    order by number 
    desc limit 6;
");


function list_date ($created) {
    // => '2017-07-01 08:08:00'
    // 如果配置文件没有配置时区
    // date_default_timezone_set('PRC');
    $timestamp = strtotime($created);
    return date('Y年m月d日', $timestamp);
  }


?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Common  &laquo; Admin</title>
    <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/assets/css/base.css">
    <link rel="stylesheet" href="/static/assets/css/index.css">
    <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
    <script src="/static/assets/vendors/jquery/jquery.min.js"></script>
    <script src="/static/assets/vendors/bootstrap/js/bootstrap.min.js"></script>
    <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
    <script>
        NProgress.start()
    </script>
    <!-- 导航栏 -->
    <?php include 'inc/navbar.php'; ?>

    <div class="bs_main container">
        <!-- 左边 -->
        <!-- 轮播图 -->
        <div class="bs_main_left">
            <div class="bs_lunbotu">
                <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                    <ol class="carousel-indicators">
                        <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                        <li data-target="#carousel-example-generic" data-slide-to="1"></li>
                        <li data-target="#carousel-example-generic" data-slide-to="2"></li>
                        <li data-target="#carousel-example-generic" data-slide-to="3"></li>
                    </ol>
                    <div class="carousel-inner cicle" role="listbox">
                        <div class="item active">
                            <a href="#" class="pc_imgBox hidden-xs"
                                style="background-image: url(/static/assets/img/lr.jpg)"></a>
                            <a href="#" class="m_imgBox hidden-lg hidden-md hidden-sm"><img
                                    src="/static/assets/img/lr.jpg" alt=""></a>
                        </div>
                        <div class="item">
                            <a href="#" class="pc_imgBox  hidden-xs"
                                style="background-image: url(/static/assets/img/lr.jpg)"></a>
                            <a href="#" class="m_imgBox hidden-lg hidden-md hidden-sm"><img
                                    src="/static/assets/img/lr.jpg" alt=""></a>
                        </div>
                        <div class="item">
                            <a href="#" class="pc_imgBox hidden-xs"
                                style="background-image: url(/static/assets/img/lr.jpg)"></a>
                            <a href="#" class="m_imgBox hidden-lg hidden-md hidden-sm"><img
                                    src="/static/assets/img/lr.jpg" alt=""></a>
                        </div>
                        <div class="item">
                            <a href="#" class="pc_imgBox hidden-xs"
                                style="background-image: url(/static/assets/img/lr.jpg)"></a>
                            <a href="#" class="m_imgBox hidden-lg hidden-md hidden-sm"><img
                                    src="/static/assets/img/lr.jpg" alt=""></a>
                        </div>

                    </div>
                    <a class="left carousel-control cicle" href="#carousel-example-generic" data-slide="prev">
                        <span class="glyphicon glyphicon-chevron-left"></span>
                    </a>
                    <a class="right carousel-control cicle" href="#carousel-example-generic" data-slide="next">
                        <span class="glyphicon glyphicon-chevron-right"></span>
                    </a>
                </div>
            </div>
            <div class="bs_list container">
                <div class="bs_list_start">
                    <img src="/static/assets/img/80ico_t.png" width="20px" height="20px" alt="">
                    <h2>最新文章</h2>
                    <em></em>
                </div>
                <div class="bs_list_end">
                    <ul class="bs_list_ul">
                        <?php foreach($list as $item):?>
                        <li>
                            <div class="bs_list_left">
                                <img src="<?php echo $item['picture']?>" alt="">
                            </div>
                            <div class="bs_list_right">
                                <h4><a href="/admin/common.php?common=<?php echo $item['id']; ?>"><?php echo $item['title']?></a></h4>
                                <span><?php echo $item['synopsis']?></span>
                            </div>
                            <div class="bs_list_look">
                                <span class="icomoon_icon icon-spinner9">&nbsp;<?php echo $item['number'];?> 浏览</span>
                                <span
                                    class="icomoon_icon icon-blogger spanmiddle">&nbsp;<?php echo $item['discussnum']?>评论</span>
                                <span
                                    class="icomoon_icon icon-filter">&nbsp;时间：<?php echo list_date($item['created'])?></span>
                            </div>
                        </li>
                        <?php endforeach?>
                    </ul>
                </div>
                <div class="bs_page">
                <ul class="pagination pagination-sm pull-right">
                    <li><a href="#">上一页</a></li>
                    <?php for( $i = $begin ; $i <= $end; $i++):?>
                    <li<?php echo $i===$page ? ' class="active"':''?>><a href="?page=<?php echo $i;?>"><?php echo $i; ?></a></li>
                    <?php endfor ?>
                        <li><a href="#">下一页</a></li>
                </ul>
            </div>
            </div>
            <!-- 页码 -->
            
        </div>


        <!-- 右边 -->
        <div class="bs_main_right ">
            <div class="bs_main_user">
                <div class="bs_main_user_top">
                    <a href="#"><img src="<?php echo $current_user['avatar']?>" alt=""></a>
                    <h3>&nbsp;<?php echo $current_user['bio']?></h3>
                </div>
                <div class="bs_main_user_middle">
                    <div class="author-social">
                        <div class="weixin"><a href="" class="icomoon_icon icon-bubbles2"></a></div>
                        <div class="qq"><a href="" class="icomoon_icon icon-tux"></a></div>
                    </div>
                </div>
                <div class="bs_main_user_button">
                    <ul>
                        <li>主页</li>
                        <li>询问</li>
                        <li>询问</li>
                    </ul>
                </div>

            </div>
            <div class="bs_main_sort container">
                <div class="bs_main_sort_start">
                    <img src="/static/assets/img/80ico_tag.png" alt="">
                    <img src="/static/assets/img/arrow-right.png" alt="">
                    <span>图书分类</span>
                </div>
                <div class="bs_main_sort_end">
                    <ul>
                        <?php foreach($book_list as $blist):?>
                        <li data-id="<?php echo $item['id']; ?>" class="btn btn-default">
                            <a href="/admin/label.php?label=<?php echo $blist['name']; ?>"><?php echo $blist['name']?></a>
                        </li>
                        <?php endforeach?>
                    </ul>
                </div>
            </div>
            <div class="bs_main_article">
                <div class="article_start">
                    <img src="/static/assets/img/80ico_tag.png" alt="">
                    <span>热门文章</span>
                </div>
                <div class="article_end">
                    <ul>
                        <?php foreach($hot_list as $item):?>
                            <li><span></span><a href="/admin/common.php?common=<?php echo $item['id']; ?>"><?php echo $item['title']?></a></li>
                        <?php endforeach?>
                       

                    </ul>
                </div>
            </div>
        </div>
    </div>

    <script>
        $('.collapse').collapse()
    </script>
    <script>
        NProgress.done()
    </script>
</body>

</html>